package chamster.utils;

import java.util.ArrayDeque;

public class FixedSizeStack<T> extends ArrayDeque<T> {
	private static final long serialVersionUID = 1L;
	private final int maxSize;
	
	public FixedSizeStack(int capacity) {
		this.maxSize = capacity;
	}
	
	@Override
	public void addFirst(T arg0) {
		this.ensureSize();
		super.addFirst(arg0);
	}
	
	@Override
	public boolean add(T arg0) {
		this.ensureSize();
		return super.add(arg0);
	}
	
	@Override
	public void addLast(T arg0) {
		this.ensureSize();
		super.addLast(arg0);
	}
	
	private void ensureSize() {
		if(super.size() >= this.maxSize) {
			super.removeLast();
		}
	}
	
	public boolean all(T that) {
		for(T entry : this) {
			if(!entry.equals(this)) {
				return false;
			}
		}
		return true;
	}
}
